<?php
defined('_JEXEC') or die();

class WrdControllerOutlets extends JControllerLegacy 
{
        protected $option;
        protected $controller; 
		function __construct( $default = array()){
    		$this->option = 'com_wrd';
    		$this->controller = 'outlets';
			parent::__construct( $default );
    	}

      	function display()
 		{
			switch($this->getTask()){
				case 'add':{
						JRequest::setVar( 'hidemainmenu', 1 );
						JRequest::setVar( 'layout', 'form'  );
						JRequest::setVar( 'view'  , 'outlets');
					} 
					break;
	            default:
					JRequest::setVar( 'view'  , 'outlets');
	            	break;
			}
			parent::display();
        }
        
      //save profile info
    function save(){
    	  
       $post=JRequest::get('post');
       $post['created_date'] = date("Y-m-d H:i:s");
       $params = JComponentHelper::getParams('com_wrd');
       $model=$this->getModel('outlets');
	   //get date current
       
	   if($post['published_cpoint'] == "on"){
			$post['published_cpoint'] = 1;
	   }
	   else {
			$post['published_cpoint'] = 0;
	   }
	  
	   $id_category=$model->store($post,'outlets');
       if($id_category!=0){
            if(!empty($post['flag_id'])){
				$this->setRedirect('index.php?option=com_wrd&controller=profile&id='.$post['flag_id'].'&layout=form');
			}else{
       			$this->setRedirect('index.php?option=com_wrd&controller=outlets','Saved');
			}
			return true;              
       }
       else{
            
            $this->setRedirect('index.php?option=com_wrd&controller=outlets','Error Saved');
            return false;
            
       }
    }    
	
	function checkOutletCode(){
	
		$code = $_POST['code'];
		$sql = "SELECT count(*) as total FROM #__wrd_outlets WHERE code ='".$code."'";
		$db = JFactory::getDbo();
		$db->setQuery($sql);
		$rs = $db->loadObject();
		echo $rs->total;
		exit();
		
	}
    function remove()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( 'Select an item to delete' ) );
		}

		$model = $this->getModel('outlets');
		if(!$model->delete($cid)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=outlets' );
	}
   
    function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>' ) );
		}

		$model = $this->getModel('outlets');
		if(!$model->publish($cid, 1)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		$this->setRedirect( 'index.php?option=com_wrd&controller=outlets' );
	}

	function unpublish(){
		
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cid = JRequest::getVar( 'cid', array(), 'post', 'array' );
		JArrayHelper::toInteger($cid);

		if (count( $cid ) < 1) {
			JError::raiseError(500, JText::_( '<p style="color:red">Please select an item to publish</p>'  ) );
		}

		$model = $this->getModel('outlets');
		
		if(!$model->publish($cid, 0)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
		}
		
		$this->setRedirect( 'index.php?option=com_wrd&controller=outlets' );
	}
}